<template>
    <div>
        <dl v-for="sector in sectorList">
            <dt><a href="javascript:;" @click="getUser(sector.id)">{{sector.title}}</a></dt>
            <dd v-for="sector2 in sector.sector2"><a href="javascript:;" @click="getUser(sector2.id)">{{sector2.title}}</a></dd>
        </dl>
        <table v-show="is_userList" border="1" align="center" width="400px">
            <tr>
                <td>名称</td>
                <td>积分</td>
                <td>操作</td>
            </tr>
            <tr v-for="user in userList">
                <td>{{user.name}}</td>
                <td>{{user.total}}</td>
                <td><button v-show="user.level>1" @click="putUser(user.id)">升级部门</button><button v-show="user.state==1" @click="logout(user.id)">下班</button></td>
            </tr>
        </table>
        <br>
        <router-link to="/record_sector">搜索</router-link>
        <br>
        <br>
        <br>
    </div>
</template>

<script>
    export default {
        name: "index_sector",
        data() {
            return {
                sectorList: [],
                userList: [],
                is_userList: false,
                sector_id: '',
            }
        },
        created() {
            this.getSector()
        },
        methods: {
            getSector() {
                this.$axios({
                    url: 'sector/sector/'
                }).then(resp => {
                    console.log(resp.data)
                    this.sectorList = resp.data
                })
            },
            getUser(id){
                this.sector_id = id;
                this.is_userList = true;
                this.$axios({
                    url: 'sector/user/',
                    params: {
                        sector_id: id
                    }
                }).then(resp=>{
                    this.userList = resp.data
                })
            },
            putUser(id){
                this.$axios({
                    url: 'sector/user/',
                    method: 'PUT',
                    data: {
                        user_id: id
                    }
                }).then(resp=>{
                    console.log(resp.data)
                })
            },
            logout(id){
                this.$axios({
                    url: 'sector/record/',
                    method: 'PUT',
                    data: {
                        user_id: id,
                        sector_id: this.sector_id
                    }
                }).then(resp=>{
                    alert(resp.data.msg);
                    if (resp.data.code==200){
                        this.$router.go(0)
                    }
                })
            }
        },
        computed: {},
        filters: {},
        watch: {},
    }
</script>

<style scoped>

</style>
